<?php

class Sys_Extensions
{	
	function FormatArray($array)
	{
		echo "<pre>";
		print_r($array);
		echo "<pre>";
		
		die;
	}
	
	function IsFromWechat(){ 
		if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
				return true;
		}	
		return false;
	}
	
	function SendVerifyCode($target_num, $verify_code)
	{
		$result = array(
			'result' => 0,
			'reason' => "未知错误"
		);
		
		$mod_params = new Db_Tables_SysParams();
		$ytx_info = $mod_params->YtxInfo();
		
		$to = $target_num;
		$datas = array($verify_code,'30');
		$tempId = $ytx_info['ytx-phoneVerify-tempId'];
		
		$accountSid = $ytx_info['ytx-accountSid'];
		$accountToken = $ytx_info['ytx-accountToken'];
		$appId = $ytx_info['ytx-appId'];
		$serverIP = 'app.cloopen.com';
		$serverPort = '8883';
		$softVersion = '2013-12-26';
		
		// 初始化REST SDK
		$rest = new Sys_SmsYtx($serverIP,$serverPort,$softVersion);
		$rest->setAccount($accountSid,$accountToken);
		$rest->setAppId($appId);

		// 发送模板短信
		$sms_result = $rest->sendTemplateSMS($to,$datas,$tempId);
		if($sms_result == NULL ) {
			$result['reason'] = "调用接口失败";
		}
		if($sms_result->statusCode!=0) {
			$result['reason'] = "Error:(".$sms_result->statusCode.")".$sms_result->statusMsg;
			//TODO 添加错误处理逻辑
		}else{
			$result = array(
				'result' => 1,
				'reason' => "发送成功"
			);
		}
	
		return $result;
	}
	
	function TurnMsgToObj($post_str)
	{
		if(!empty($post_str))
		{
			$result = simplexml_load_string($post_str, 'SimpleXMLElement', LIBXML_NOCDATA);
		}else{
			$result = NULL;
		}
		
		return $result;
	}
	
	function WelcomeMsg($postObj)
	{
		$mod_params = new Db_Tables_SysParams();
		$wechat_welcome_info = $mod_params->GetVal('wechat_welcome_msg_text');
		$toUser = $postObj->FromUserName;
		$fromUser = $postObj->ToUserName;
		$createTime = time();
		
		$result = "<xml>
						<ToUserName><![CDATA[".$toUser."]]></ToUserName>
						<FromUserName><![CDATA[".$fromUser."]]></FromUserName>
						<CreateTime>".$createTime."</CreateTime>
						<MsgType><![CDATA[text]]></MsgType>
						<Content><![CDATA[".$wechat_welcome_info."]]></Content>
					</xml>";
		
		return $result;
	}
}